package com.hdcx.Minfresh.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.adapter.MineMyPointAdapter;
import com.hdcx.Minfresh.bean.MyPointWrapper;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.hdcx.Minfresh.view.FullyLinearLayoutManager;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class MineMyPointActivity extends BaseActivity {
    RecyclerView recyclerview;
    MineMyPointAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_point);
        initView();
        initData();
    }

    private void initView() {
        recyclerview = (RecyclerView) findViewById(R.id.recycler);
        adapter = new MineMyPointAdapter(this);
        recyclerview.setLayoutManager(new FullyLinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recyclerview.setAdapter(adapter);
        findViewById(R.id.tv_left).setOnClickListener(this);
        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("我的积分");
    }
    private void initData() {
        post_main();
    }
 /*   uid   //用户id  20
    page	页码
    size    每页数量  可选*/
    private void post_main() {
        AppUtil.showLoading(this);
        UserInfoBean user =  ShpfUtil.getObject(ShpfUtil.LOGIN);
        JSONObject json = new JSONObject();
        try {
            json.put("uid", user.getUserinfo().getId());
            json.put("page", "1");
            json.put("size", "10");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.SCORELIST)
                .content(json.toString())
                .build()
                .execute(new Callback<MyPointWrapper>() {
                    @Override
                    public MyPointWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, MyPointWrapper.class);
                    }
                    @Override
                    public void onError(Call call, Exception e) {
                        AppUtil.closeLoading();
                    }

                    @Override
                    public void onResponse(MyPointWrapper response) {
                        AppUtil.closeLoading();
                        if (response.getStatus() == 1) {
                            adapter.setData(response);
                        }
                    }
                });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_left:
                finish();
                break;
        }
    }

}
